package com.zywl.utils.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * Description:文件上传工具类
 *
 * @author yangfan
 * @date 2017年12月20日 下午1:16:04
 */
public class FileUploadUtil {
    /**
     * 单文件上传，layui上传方式
     *
     * @param request
     * @param file
     * @param fileFolderName
     * @return
     * @throws Exception
     */
    public static String fileUpload(HttpServletRequest request, MultipartFile file, String fileFolderName) throws Exception {
        String fileName = file.getOriginalFilename();
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotEmpty(fileName)) {
            char c = (char) (int) (Math.random() * 26 + 97);
            sb.append(c);
            c = (char) (int) (Math.random() * 26 + 97);
            sb.append(c);
            sb.append(Constants.STRING_UNDERLINE);
            sb.append(DateUtil.nowByFormate(Constants.DEFAULT_FORMAT));
            sb.append(fileName.substring(fileName.lastIndexOf(Constants.STRING_POINT)));
            fileName = sb.toString();

            String realPath = request.getSession().getServletContext().getRealPath(File.separator);
            String path = request.getContextPath();
            if (StringUtils.isNotEmpty(path)) {
                path = path.replace(Constants.STRING_SPRIT, Constants.STRING_NULL);
            } else {
                path = Constants.ROOT;
            }
            sb.insert(0, File.separator);
            sb.insert(0, fileFolderName);
            sb.insert(0, realPath.substring(0, realPath.indexOf(path)));
            String ctxPath = sb.toString();

            File newFile = FileUtil.newFile(ctxPath);
            file.transferTo(newFile);

            sb.delete(0, sb.length());
            sb.append(fileFolderName);
            sb.append(Constants.STRING_SPRIT);
            sb.append(fileName);
        }
        return sb.toString();
    }

    /**
     * 多文件上传，layui上传方式
     *
     * @param request
     * @param files
     * @param fileFolderName
     * @return
     * @throws Exception
     */
    public static String filesUpload(HttpServletRequest request, MultipartFile[] files, String fileFolderName) throws Exception {
        String fileName = null;
        String filePathes = null;
        StringBuilder sb = new StringBuilder();
        for (MultipartFile file : files) {
            fileName = file.getOriginalFilename();
            if (StringUtils.isNotEmpty(fileName)) {
                char c = (char) (int) (Math.random() * 26 + 97);
                sb.append(c);
                c = (char) (int) (Math.random() * 26 + 97);
                sb.append(c);
                sb.append(Constants.STRING_UNDERLINE);
                sb.append(DateUtil.nowByFormate(Constants.DEFAULT_FORMAT));
                sb.append(fileName.substring(fileName.lastIndexOf(Constants.STRING_POINT)));
                fileName = sb.toString();

                String realPath = request.getSession().getServletContext().getRealPath(File.separator);
                String path = request.getContextPath();
                if (StringUtils.isNotEmpty(path)) {
                    path = path.replace(Constants.STRING_SPRIT, Constants.STRING_NULL);
                } else {
                    path = Constants.ROOT;
                }
                sb.insert(0, File.separator);
                sb.insert(0, fileFolderName);
                sb.insert(0, realPath.substring(0, realPath.indexOf(path)));
                String ctxPath = sb.toString();

                File newFile = FileUtil.newFile(ctxPath);
                file.transferTo(newFile);

                sb.delete(0, sb.length());
                sb.append(Constants.STRING_SPLIT);
                sb.append(fileFolderName);
                sb.append(Constants.STRING_SPRIT);
                sb.append(fileName);
            }
        }
        filePathes = sb.toString();
        if (StringUtils.isNotEmpty(filePathes)) {
            filePathes = filePathes.substring(1);
        }
        return filePathes;
    }

    /**
     * 单文件上传
     *
     * @param request
     * @param fileId         页面上传文件name
     * @param fileFolderName 文件上传后文件夹名
     * @return fileFolderName/fileName
     * @throws Exception
     */
    public static String fileUpload(HttpServletRequest request, String fileId, String fileFolderName) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(fileId);

        String fileName = file.getOriginalFilename();
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotEmpty(fileName)) {
            char c = (char) (int) (Math.random() * 26 + 97);
            sb.append(c);
            sb.append(Constants.STRING_UNDERLINE);
            sb.append(DateUtil.nowByFormate(Constants.DEFAULT_FORMAT));
            sb.append(fileName.substring(fileName.lastIndexOf(Constants.STRING_POINT)));
            fileName = sb.toString();

            String realPath = request.getSession().getServletContext().getRealPath(File.separator);
            String path = request.getContextPath();
            if (StringUtils.isNotEmpty(path)) {
                path = path.replace(Constants.STRING_SPRIT, Constants.STRING_NULL);
            } else {
                path = Constants.ROOT;
            }
            sb.insert(0, File.separator);
            sb.insert(0, fileFolderName);
            sb.insert(0, realPath.substring(0, realPath.indexOf(path)));
            String ctxPath = sb.toString();

            File newFile = FileUtil.newFile(ctxPath);
            file.transferTo(newFile);

            sb.delete(0, sb.length());
            sb.append(fileFolderName);
            sb.append(Constants.STRING_SPRIT);
            sb.append(fileName);
        }
        return sb.toString();
    }

    /**
     * 单文件上传压缩包，并解压，仅限.zip压缩包
     *
     * @param request
     * @param fileId         页面上传文件name
     * @param fileFolderName 文件上传后文件夹名
     * @return fileFolderName/fileName
     * @throws Exception
     */
    public static String packageUpload(HttpServletRequest request, String fileId, String fileFolderName)
            throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(fileId);

        String fileName = file.getOriginalFilename();
        StringBuilder sb = new StringBuilder();
        String pics = Constants.STRING_NULL;
        if (StringUtils.isNotEmpty(fileName)) {
            long now = System.currentTimeMillis();
            char c = (char) (int) (Math.random() * 26 + 97);
            sb.append(c);
            sb.append(Constants.STRING_UNDERLINE);
            sb.append(DateUtil.nowByFormate(Constants.DEFAULT_FORMAT));
            sb.append(fileName.substring(fileName.lastIndexOf(Constants.STRING_POINT)));
            fileName = sb.toString();

            sb.delete(0, sb.length());
            String realPath = request.getSession().getServletContext().getRealPath(File.separator);
            String path = request.getContextPath();
            if (StringUtils.isNotEmpty(path)) {
                path = path.replace(Constants.STRING_SPRIT, Constants.STRING_NULL);
            } else {
                path = Constants.ROOT;
            }
            sb.append(realPath.substring(0, realPath.indexOf(path)));
            String ctxPath = sb.toString();

            File newFile = FileUtil.newFile(ctxPath + now + File.separator + fileName);
            file.transferTo(newFile);

            sb.append(now);
            sb.append(File.separator);
            String tempPath = sb.toString();

            List<File> files = ZipUtil.unZip(tempPath + fileName, tempPath, true);
            int count = 0;
            for (File f : files) {
                count++;
                String name = f.getName();
                sb.delete(0, sb.length());
                c = (char) (int) (Math.random() * 26 + 97);
                sb.append(c);
                sb.append(count);
                sb.append(Constants.STRING_UNDERLINE);
                sb.append(now);
                sb.append(name.substring(name.lastIndexOf(Constants.STRING_POINT)));
                fileName = fileFolderName + File.separator + sb.toString();
                FileUtil.copyFile(f, new File(ctxPath + fileName));
                pics += Constants.STRING_SPLIT + fileFolderName + Constants.STRING_SPRIT + sb.toString();
                ;
            }
            pics = pics.substring(1);
            FileUtil.deleteFolder(new File(tempPath));
        }
        return pics;
    }

    /**
     * 多文件上传
     *
     * @param request
     * @param fileId
     * @param fileFolderName
     * @return
     * @throws Exception
     */
    public static String filesUpload(HttpServletRequest request, String fileId, String fileFolderName)
            throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> files = multipartRequest.getFiles(fileId);
        StringBuilder sb = new StringBuilder();
        String filePathes = Constants.STRING_NULL;

        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();

            if (StringUtils.isNotEmpty(fileName)) {
                sb.delete(0, sb.length());
                char c = (char) (int) (Math.random() * 26 + 97);
                sb.append(c);
                sb.append(Constants.STRING_UNDERLINE);
                sb.append(DateUtil.nowByFormate(Constants.DEFAULT_FORMAT));
                sb.append(fileName.substring(fileName.lastIndexOf(".")));
                fileName = sb.toString();

                String realPath = request.getSession().getServletContext().getRealPath(File.separator);
                String path = request.getContextPath().replace(Constants.STRING_SPRIT, Constants.STRING_NULL);
                path = StringUtils.isNotEmpty(path) ? path : Constants.ROOT;
                sb.insert(0, Constants.STRING_SPRIT);
                sb.insert(0, fileFolderName);
                sb.insert(0, realPath.substring(0, realPath.indexOf(path)));
                String ctxPath = sb.toString();

                File newFile = FileUtil.newFile(ctxPath);
                file.transferTo(newFile);

                sb.delete(0, sb.length());
                sb.append(Constants.STRING_SPLIT);
                sb.append(fileFolderName);
                sb.append(Constants.STRING_SPRIT);
                sb.append(fileName);

                filePathes += sb.toString();
            }
        }

        if (!Constants.STRING_NULL.equals(filePathes)) {
            filePathes = filePathes.substring(1);
        }

        return filePathes;
    }
}
